import { AssertServiceBase } from '../../contract';

export class EditorAssertService extends AssertServiceBase {
    public constructor(
        private m_View: string,
    ) {
        super();

        this.cwd = `db://assets/${this.m_View}`;
    }

    public async refresh(relativePath: string) {
        let assetPath = this.cwd + '/' + relativePath;
        await Editor.Message.request('asset-db', 'refresh-asset', assetPath);
    }

    public async load(relativePath: string, extra?: string) {
        await this.refresh(relativePath);

        const pathArgs = [this.cwd, relativePath];
        if (extra)
            pathArgs.push(extra);
        const uuid = await Editor.Message.request(
            'asset-db',
            'query-uuid',
            pathArgs.join('/'),
        ) as string;
        return new Promise<any>((s, f) => {
            require('cc').assetManager.loadAny([{
                type: 'uuid',
                uuid
            }], null, (err: Error, res: any) => {
                if (err)
                    return f(err);

                s(res);
            });
        });
    }
}